package com.ywj;

import com.ywj.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * Mybatis 快速入门代码
 */
public class MyBatisDemo {
    public static void main(String[] args) throws IOException {
//        1）定义POJO类             对应user表
//        2）加载核心配置文件，获取SqlSessionFactory对象
//        3）获取SqlSession对象，执行SQL语句
//        4）释放资源
        //1.加载mybatis的核心配置文件，获取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        System.out.println(System.getProperty("user.dir"));

        //2.获取SqlSession对象，用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //3.执行sql。传sql语句的唯一标识，namespace.id
        List<User> users = sqlSession.selectList("test.selectAll");

        System.out.println(users);
        sqlSession.close();
    }
}
